import pymysql

# 判断一个字符串能否被转换为一个有效的数字
def check_number(string):
    point = 0
    isvalid = True
    for c in string:
        code = ord(c)
        if (code < 48 and code != 46) or code > 57:
            isvalid = False
            break
        elif code == 46:
            point += 1
    if point > 1: isvalid = False
    return isvalid

# 创建数据库连接对象
def create_obj():
    db = pymysql.connect("localhost","root","","woniubook",charset="utf8")
    cur = db.cursor()
    return db,cur

# 对数据库的增、删、改
def db_operation(sql):
    db,cur = create_obj()
    cur.execute(sql)
    db.commit()
    cur.close()
    db.close()

# 对数据库的查询(一条数据）
def query_one(sql):
    db,cur = create_obj()
    cur.execute(sql)
    db.commit()
    content = cur.fetchone()
    cur.close()
    db.close()
    return content

# 对数据库的查询(所有数据）
def query_all(sql):
    db,cur = create_obj()
    cur.execute(sql)
    db.commit()
    content = cur.fetchall()
    cur.close()
    db.close()
    return content
